package com.example.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Menu;
import com.example.demo.entity.Role;
import com.example.demo.mapper.MenuMapper;
import com.example.demo.service.IMenuService;
import com.example.demo.utils.TreeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wzz
 * @since 2023-09-23
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {
    @Resource
    private MenuMapper menuMapper;
    public boolean saveUser(Menu menu) {
        return saveOrUpdate(menu);
    }
    public List<Menu> getDataById(String id){
        return menuMapper.getDataById(id);
    }

    public List<Menu> findMenus() {
        // 查出所有数据
        List<Menu> menus =  this.list();
        // 找出一级菜单
        List<Menu> parentNode =  menus.stream().filter(menu->menu.getPid() == null).collect(Collectors.toList());
        for (Menu menu :parentNode){
            menu.setChildren(menus.stream().filter(m->menu.getId().equals(m.getPid())).collect(Collectors.toList()));
        }
//        List<Menu> parentNode = TreeUtil.build(menus);
        return parentNode;
    }
}
